﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BLL;

namespace ChamCongMain.Form
{
    public partial class frmHangSanPham : DevExpress.XtraEditors.XtraForm
    {
        ChucNang cn;
        public Delegates.RefeshGrid LoadData;
        long id = 0;
        public frmHangSanPham(ChucNang cn, HangSanPham hsp)
        {
            InitializeComponent();
            this.cn = cn;
            if (this.cn == ChucNang.Them)
            {
                this.Text = "Thêm Hãng Sản Phẩm";
            }
            else
            {
                this.Text = "Sửa Hãng Sản Phẩm";
                SetValueForForm(hsp);
            }
        }

        /// <summary>
        /// Lay du lieu de luu vao ojbect hangSP
        /// </summary>
        private HangSanPham GetValueFromForm()
        {
            HangSanPham hangSP = new HangSanPham();
            hangSP.HangSPId = id;
            hangSP.TenHang=txtTenHangSP.Text;
            hangSP.GhiChu = txtGhiChu.Text;
            return hangSP;
        }

        /// <summary>
        /// Dua du lieu len form khi sua
        /// </summary>
        /// <param name="hsp"></param>
        private void SetValueForForm(HangSanPham hsp)
        {
            id = hsp.HangSPId;
            txtTenHangSP.Text = hsp.TenHang;
            txtGhiChu.Text = hsp.GhiChu;
        }
        
        /// <summary>
        /// Xoa trang cac textbox sau khi them thanh cong
        /// </summary>
        private void ClearForm()
        {
            txtTenHangSP.Text = string.Empty;
            txtGhiChu.Text = string.Empty;
            txtTenHangSP.Focus();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            HangSanPham hsp = GetValueFromForm();
            HangSanPhamBLL hspbll = new HangSanPhamBLL();
            if (cn == ChucNang.Them)
            {
                hspbll.InsertHangSanPham(hsp);
                LoadData(hspbll.GetAllHangSanPham());
                XtraMessageBox.Show("Thêm thành công!", "Thêm", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                hspbll.UpdateHangSanPham(hsp);
                LoadData(hspbll.GetAllHangSanPham());
                XtraMessageBox.Show("Sửa thành công!", "Sửa", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            this.Close();
        }

        private void btnLuuThem_Click(object sender, EventArgs e)
        {
            HangSanPham hsp = GetValueFromForm();
            HangSanPhamBLL hspbll = new HangSanPhamBLL();
            if (cn == ChucNang.Them)
            {
                hspbll.InsertHangSanPham(hsp);
                LoadData(hspbll.GetAllHangSanPham());
                XtraMessageBox.Show("Thêm thành công!", "Thêm", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                hspbll.UpdateHangSanPham(hsp);
                LoadData(hspbll.GetAllHangSanPham());
                XtraMessageBox.Show("Sửa thành công!", "Sửa", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            ClearForm();
        }

        private void btnHuyBo_Click(object sender, EventArgs e)
        {
            DialogResult re = XtraMessageBox.Show("Bạn có muốn thoát không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (re == DialogResult.Yes)
                this.Close();
        }


    }
}